package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	/**
	 * 获取分类数据
	 * @return
	 */
	@Override
	public List<JSONObject> getBaseCategoryList() {
		//创建集合
		List<JSONObject> list=new ArrayList<>();
		//查询视图所有数据
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
		//获取一级分类 分组
		Map<Long, List<BaseCategoryView>> map = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		while (iterator.hasNext()){
			//获取数据
			Map.Entry<Long, List<BaseCategoryView>> next = iterator.next();
			Long key = next.getKey();
			List<BaseCategoryView> value = next.getValue();
			//创建一级分类对象
			JSONObject category1 = new JSONObject();
			category1.put("categoryId",key);
			category1.put("categoryName",value.get(0).getCategory1Name());
			//获取二级分类
			Map<Long, List<BaseCategoryView>> map1 = value.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map1.entrySet().iterator();
			//二级分类集合
			List<JSONObject> category2List = new ArrayList<>();
			while (iterator1.hasNext()){
				//获取二级分类数据
				Map.Entry<Long, List<BaseCategoryView>> next1 = iterator1.next();
				Long key1 = next1.getKey();
				List<BaseCategoryView> value1 = next1.getValue();
				//创建二级分类对象
				JSONObject category2 = new JSONObject();
				category2.put("categoryId",key1);
				category2.put("categoryName",value1.get(0).getCategory2Name());
				//获取三级分类数据
				List<JSONObject> category3List = value1.stream().map(baseCategoryView -> {
					//创建三级分类数据
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//将三级分类数据添加到二级分类对象中
				category2.put("categoryChild",category3List);
				//将二级分类数据添加到二级分类集合中
				category2List.add(category2);
			}
			//将二级分类集合添加到一级分类对象中
			category1.put("categoryChild",category2List);
			//将一级分类添加到集合中
			list.add(category1);
		}
		return list;
	}


	/**
	 * 根据一级分类id查询属性标签数据
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.findAttribute(category1Id);
	}

	/**
	 * 根据三级分类id查询分类数据
	 * @param category3Id
	 * @return
	 */
	@Override
	@TsCache(prefix = "category:view:")
	public BaseCategoryView getCategoryView(Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
		return baseCategoryView;
	}

	/**
	 * 根据一级分类id查询数据
	 * @param category1Id
	 * @return
	 */
	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//创建一级分类集合
		JSONObject category1 = new JSONObject();
		//根据一级分类id获取视图
		List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView::getCategory1Id, category1Id));
		//赋值
		category1.put("categoryId",category1Id);
		category1.put("categoryName",baseCategoryViews.get(0).getCategory1Name());
		//获取二级分类数据
		Map<Long, List<BaseCategoryView>> collect = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = collect.entrySet().iterator();
		//创建二级分类集合
		List<JSONObject> category2List = new ArrayList<>();
		while (iterator.hasNext()){
			Map.Entry<Long, List<BaseCategoryView>> next = iterator.next();
			Long key = next.getKey();
			List<BaseCategoryView> value = next.getValue();
			//创建二级分类对象
			JSONObject category2 = new JSONObject();
			//赋值
			category2.put("categoryId",key);
			category2.put("categoryName",value.get(0).getCategory2Name());
			//获取三级分类数据
			List<JSONObject> list = baseCategoryViews.stream().map(baseCategoryView -> {
				//创建三级分类对象
				JSONObject category3 = new JSONObject();
				//赋值
				category3.put("categoryId", baseCategoryView.getCategory3Id());
				category3.put("categoryName", baseCategoryView.getCategory3Name());
				return category3;
			}).collect(Collectors.toList());
			//将三级分类数据添加到二级分类对象中
			category2.put("categoryChild",list);
			//将二级分类数据添加到二级分类集合中
			category2List.add(category2);
		}
		//将二级分类集合添加到一级分类对象中
		category1.put("categoryChild",category2List);
		return category1;
	}

	/**
	 * 根据一级分类id查询三级分类数据
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//	根据一级分类Id 查询二级分类数据Id 集合
		List<Long> category2IdList = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id)).stream().map(BaseCategory2::getId).collect(Collectors.toList());
		LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
		wrapper.in(BaseCategory3::getCategory2Id,category2IdList).eq(BaseCategory3::getIsTop,1).last(" limit 7 ");
		return baseCategory3Mapper.selectList(wrapper);
	}

	/**
	 * 查询所有一级分类数据
	 * @return
	 */
	@Override
	public List<BaseCategory1> findAllCategory1() {
		return baseCategory1Mapper.selectList(null);
	}
}
